﻿using System;
using System.Configuration;
using System.Net.Mail;
using System.Text;

namespace SBACN.WebSite.Common
{
    delegate void SendMailSync(string title, string content, string toList);

	public class Mail
	{
		public static Mail Instance = new Mail();
		private string MailSmtpServer
        {
            get
            {
                return ConfigurationManager.AppSettings["MailSmtpServer"] ?? "";
            }
        }
		private string MailFromAddress
        {
            get
            {
                return ConfigurationManager.AppSettings["MailFromAddress"] ?? "";
            }
        }
		private string MailFromUserName
        {
            get
            {
                return ConfigurationManager.AppSettings["MailFromUserName"] ?? "";
            }
        }
		private string MailFromPassword
        {
            get
            {
                return ConfigurationManager.AppSettings["MailFromPassword"] ?? "";
            }
        }
		private int SmtpPort
		{
			get
			{
				int port = 25;
				int.TryParse(ConfigurationManager.AppSettings["MailSmtpServerPort"], out port);
				return port;
			}
		}
		private bool IsCredentials
		{
			get
			{
				bool isCredentials = false;
				if (!bool.TryParse(ConfigurationManager.AppSettings["MailSmtpIsCredentials"], out isCredentials))
				{
					if (!string.IsNullOrWhiteSpace(ConfigurationManager.AppSettings["MailSmtpIsCredentials"]))
					{
						isCredentials = ConfigurationManager.AppSettings["MailSmtpIsCredentials"].ToLower() == "yes" || ConfigurationManager.AppSettings["MailSmtpIsCredentials"] == "1" || ConfigurationManager.AppSettings["MailSmtpIsCredentials"].ToLower() == "y";
					}
				}
				return isCredentials;
			}
		}
		private bool IsEnableSsl
		{
			get
			{
				bool isEnableSsl = false;
				if (!bool.TryParse(ConfigurationManager.AppSettings["MailSmtpIsEnableSsl"], out isEnableSsl))
				{
					if (!string.IsNullOrWhiteSpace(ConfigurationManager.AppSettings["MailSmtpIsEnableSsl"]))
					{
						isEnableSsl = ConfigurationManager.AppSettings["MailSmtpIsEnableSsl"].ToLower() == "yes" || ConfigurationManager.AppSettings["MailSmtpIsEnableSsl"] == "1" || ConfigurationManager.AppSettings["MailSmtpIsEnableSsl"].ToLower() == "y";
					}
				}
				return isEnableSsl;
			}
		}
		private Mail() 
		{
		}

		public void SendEMail(string title, string content, params string[] toList)
		{
            string to = string.Empty;
            try
            {
                if (toList == null || toList.Length == 0) return;
                StringBuilder builder = new StringBuilder();
                foreach (string item in toList)
                {
                    builder.Append(";");
                    builder.Append(item);
                }
                to = builder.Length > 0 ? builder.Remove(0, 1).ToString() : string.Empty;
                SendMailSync acs = new SendMailSync(SendMainSyncMethod);
                IAsyncResult ia = acs.BeginInvoke(title, content, to, null, null);
            }
            catch (Exception ex)
            {
                Log.Instance.WriteErrorLog(string.Format("发送邮件异常;title:{0}; content:{1};toList:{2}", title, content, to), ex);
            }
		}

        private void SendMainSyncMethod(string title, string content, string toList)
        {
			SmtpClient client = new SmtpClient(MailSmtpServer, SmtpPort);
			client.UseDefaultCredentials = IsCredentials;
			client.Credentials = new System.Net.NetworkCredential(MailFromAddress, MailFromPassword);
			client.DeliveryMethod = SmtpDeliveryMethod.Network;
			client.EnableSsl = IsEnableSsl;

            MailMessage message = new MailMessage(MailFromAddress, toList, title, content);
			message.From = new MailAddress(MailFromAddress, MailFromUserName, Encoding.UTF8);
            message.BodyEncoding = System.Text.Encoding.UTF8;
            message.IsBodyHtml = true;
            client.Send(message);
        }
	}
}